﻿using System;
using System.IO;
using System.Text;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using DotNetLib.Windows.Forms;


namespace EpisodeWizard.App
{

	/// <summary>
	/// Displays all the files to be processed, along with values and guesses
	/// </summary>
	public partial class FileTree : UserControl
	{

		public FileTree()
		{
			InitializeComponent();
		}


		/// <summary>
		/// Creates the file tree
		/// </summary>
		public void CreateTree()
		{
			// Get all folders
			Dictionary<string, string> Folders = new Dictionary<string, string>();
			foreach (Core.EpisodeFile EF in Core.Program.GetInstance().EpisodeFiles)
			{
				string RelativePath = EF.Filepath.Substring(EF.RootFolder.Length+1);
				string Folder = Path.GetDirectoryName(RelativePath);
				try
				{
					Folders.Add(Folder, "");
				}
				catch (ArgumentException)
				{
					// Folder already added
				}
			}

			int NbFiles = 0;
			int NbFilesWithWarnings = 0;

			treelistView.Items.Clear();
			foreach (string Folder in Folders.Keys)
			{
				ContainerListViewItem FolderNode = new ContainerListViewItem();
				FolderNode.Text = Folder;
				FolderNode.ImageIndex = 0;
				treelistView.Items.Add(FolderNode);

				foreach (Core.EpisodeFile EF in Core.Program.GetInstance().EpisodeFiles)
				{
					string RelativePath = EF.Filepath.Substring(EF.RootFolder.Length + 1);
					if (Folder == Path.GetDirectoryName(RelativePath))
					{
						++NbFiles;

						// Display all files or only those with warnings?
						bool Display = true;
						if (radioDisplayOnlyWarnings.Checked)
						{
							Display = HasWarnings(EF);
						}

						if (Display)
						{
							++NbFilesWithWarnings;

							ContainerListViewItem FileNode = new ContainerListViewItem();
							FileNode.Text = Path.GetFileName(RelativePath);
							FileNode.ImageIndex = 1;
							FileNode.SubItems.Add(new ContainerListViewSubItem(0));
							FileNode.SubItems.Add(new ContainerListViewSubItem(1));
							FileNode.SubItems.Add(new ContainerListViewSubItem(2));
							FileNode.SubItems.Add(new ContainerListViewSubItem(3));
							FileNode.Tag = EF;

							FolderNode.Items.Add(FileNode);
						}
					}
				}

				if (FolderNode.Items.Count == 0)
				{
					treelistView.Items.Remove(FolderNode);
				}
			}

			ExpandAll(treelistView.Items);

			// Show the values and guesses for each entry
			UpdateValues();

			// Number of files
			labelNbFiles.Text = NbFiles + " files / " + NbFilesWithWarnings + " warnings";
		}


		/// <summary>
		/// Expands the whole treelist
		/// </summary>
		private void ExpandAll(ContainerListViewItemCollection Items)
		{
			foreach (ContainerListViewItem Child in Items)
			{
				ExpandAll(Child.Items);
				Child.Expand();
			}
		}


		/// <summary>
		/// Returns true if the file has at least one warning
		/// i.e. 0 choice or more than 1 choice
		/// </summary>
		private bool HasWarnings(Core.EpisodeFile EF)
		{
			if (EF.PossibleShows.SelectedValue == null)
				return true;
			if (EF.PossibleEpisodes.SelectedValue == null)
				return true;
			return false;
		}


		/// <summary>
		/// Updates the values in the tree
		/// Does not re-crate the nodes
		/// </summary>
		public void UpdateValues()
		{
			foreach (ContainerListViewItem FolderNode in treelistView.Items)
			{
				foreach (ContainerListViewItem FileNode in FolderNode.Items)
				{
					Core.EpisodeFile EF = (Core.EpisodeFile)FileNode.Tag;

					// Filename
					FileNode.SubItems[0].ForeColor = Color.Green;

					// Guesses
					DisplayGuess(FileNode.SubItems[1], EF.PossibleShows);
					DisplayGuess(FileNode.SubItems[2], EF.PossibleEpisodes);
					DisplayGuess(FileNode.SubItems[3], EF.PossibleTitles);

					// New filename
					if (EF.NewFilename != null)
					{
						FileNode.SubItems[4].Text = EF.NewFilename;
						FileNode.SubItems[4].ForeColor = Color.Green;
					}
					else
					{
						FileNode.SubItems[4].Text = "?";
						FileNode.SubItems[4].ForeColor = Color.Red;
					}
				}
			}

			treelistView.Invalidate();
		}


		/// <summary>
		/// Display the best guess in a list subitem
		/// </summary>
		void DisplayGuess<T>(ContainerListViewSubItem SubItem, Core.Guess<T> XGuess)
		{
			if (XGuess.SelectedValue != null)
			{
				SubItem.Text = XGuess.SelectedValue.ToString();
				SubItem.ForeColor = Color.Green;
			}
			else if (XGuess.Choices.Count > 0)
			{
				SubItem.Text = XGuess.Choices.Count + " choices";
				SubItem.ForeColor = Color.Orange;
			}
			else
			{
				SubItem.Text = "?";
				SubItem.ForeColor = Color.Red;
			}
		}

		private void radioGroupFolders_CheckedChanged(object sender, EventArgs e)
		{
			CreateTree();
		}

		private void radioFullHierarchy_CheckedChanged(object sender, EventArgs e)
		{
			CreateTree();
		}

		/// <summary>
		/// Open file properties for selected episode
		/// </summary>
		private void treelistView_DoubleClick(object sender, EventArgs e)
		{
			ShowProperties();
		}

		/// <summary>
		/// Show properties for the selected file
		/// </summary>
		private void menuProperties_Click(object sender, EventArgs e)
		{
			ShowProperties();
		}


		private void ShowProperties()
		{
			if (treelistView.SelectedItems.Count > 0)
			{
				Core.EpisodeFile EF = (Core.EpisodeFile)treelistView.SelectedItems[0].Tag;

				FileGuesses Dlg = new FileGuesses();
				Dlg.SetEpisodeFile(EF);
				Dlg.ShowDialog();
			}
		}

		/// <summary>
		/// Set the show for all selected files
		/// </summary>
		private void menuSelectShow_Click(object sender, EventArgs e)
		{

		}

		/// <summary>
		/// Play the selected episode file using default player
		/// </summary>
		private void menuPlay_Click(object sender, EventArgs e)
		{

		}

	}
}

